var tpl = "my name is <%this.name%>. I am <%this.profile.age%> years old.";


var data = {
    name: "Krasimir",
    profile: {
        age: 29,
    }
};

function tmp01 () {
    return "my name is " + this.name + '. I am ' + this.profile.age + ' years old.';
}

function tmp02 () {
    let code = []
    code.push('my name is ')
    code.push(this.name)
    code.push('. I am ')
    code.push(this.profile.age)
    code.push(' years old.')
    return code.join('');
}

function tmp03 () {
    var code  = `
    let code = []
    code.push('my name is ')
    code.push(this.name)
    code.push('. I am ')
    code.push(this.profile.age)
    code.push(' years old.')
    return code.join('');
    `
    return new Function(code)
}

function temp04 () {
    var re = /<%([\w$.]+)?%>/g;
    let code = 'let code = []\n';
    let match = null;
    let cursor = 0;
    var add = function(line, isJS) {
        if (isJS) {
            code += `code.push(${line})\n`;
        } else {
            code += `code.push('${line}')\n`;
        }
    }
    while(match = re.exec(tpl)) {
        let substr = tpl.slice(cursor, match.index);
        add(substr)
        add(match[1], true)
        cursor = re.lastIndex;
    }
    add(tpl.slice(cursor));
    code += "return code.join('')";
    // console.log(code);
    return new Function(code);
}
// temp04();
function render (tpl, data) {
    return temp04().call(data)
}

console.log(render(tpl, data))